define(['jquery', 'bootstrap', 'backend', 'table', 'form',], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/index',
                    add_url: 'activity/add',
                    edit_url: 'activity/edit',
                    del_url: 'activity/del',
                    multi_url: 'activity/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk:'id',
                sortName:'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                       // {field: 'fx_ewm', title: __('fx_ewm'), formatter: Table.api.formatter.image, events: Table.api.events.image},
                        {field: 'img', title: '图片', formatter: Table.api.formatter.image, events: Table.api.events.image},
                        {field: 'title', title: __('title'),width:'200px'},
                        {field: 'hotelname', title: __('hotelname'),width:'400px'},
                        {field: 'price', title: __('price')},
                        {field: 'preprice', title: __('preprice')},
                        {field: 'phone', title: __('phone')},
                        {field: 'sell', title: __('sell')},
                        {field: 'stock', title: __('stock')},
                        {field: 'address', title: __('address')},
                        {field: 'enddate', title: __('enddate'),},
                        {field: 'hot', title: __('hot'),operate: false, searchList:{"0":__('hot0'),"1":__('hot1'),},formatter:Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ],
                commonSearch: false,

            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //修改添加是弹窗大小
            $(".btn-add").data("area", ["80%","80%"]); //自定义弹窗大小
            //修改编辑弹窗大小
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["80%","80%"]); //自定义弹窗大小
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },

        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);

            //省切换
            $("#h_areaide").change(function () {
                $("#shiid option").css("display",'none');
                $("#shiid option[pid='0']").prop("selected",true);
                var sel = $(this).val();
                $("#shiid option[pid="+ sel +"]").css("display", "block");
                if(sel){

                    $("#shiid").css("display","block");
                    $("#area").css("display","none");
                    $("#areasel label input").prop("checked",false);
                }else{
                    $("#shiid").css("display","none");

                }

            })

            //市切换
            $("#h_areadid").change(function () {
                var sel = $(this).val();
                $("#areasel label").css("display",'none');
                $("#areasel label input").prop("checked",false);
                if(sel){
                    $("#areasel label[pid="+ sel +"]").css("display", "inline-block");
                    $("#area").css("display","block");
                }else{
                    $("#area").css("display","none");
                }
            });

        },
        edit: function () {
            //选中城市
            var sel = $("#h_areaide").attr("ids");
            var sel2 = $("#h_areadid").attr("ids");
            $("#h_areaide option[value="+ sel +"]").prop("selected",true);
            $("#shiid option").css("display",'none');
            $("#shiid option[pid="+ sel +"]").css("display", "block");
            $("#h_areadid option[value="+ sel2 +"]").prop("selected",true);

            //省切换
            $("#h_areaide").change(function () {
                $("#shiid option").css("display",'none');
                $("#shiid option[pid='0']").prop("selected",true);
                var sel = $(this).val();
                $("#shiid option[pid="+ sel +"]").css("display", "block");
                if(sel){
                    $("#shiid").css("display","block");
                    $("#area").css("display","none");
                    $("#areasel label input").prop("checked",false);
                }else{
                    $("#shiid").css("display","none");
                }

            })
            //市切换
            $("#h_areadid").change(function () {
                var sel = $(this).val();
                $("#areasel label").css("display",'none');
                $("#areasel label input").prop("checked",false);
                if(sel){
                    $("#areasel label[pid="+ sel +"]").css("display", "inline-block");
                    $("#area").css("display","block");
                }else{
                    $("#area").css("display","none");
                }

            });


            //选择酒店
            var oldhotelid = $("#oldreactids").val();
            var h_arr = new Array(); //定义一数组
            h_arr = oldhotelid.split(",");

            for (i=0;i<h_arr.length;i++ )
            {
                $("#reactids option").each(function () {
                    if($(this).val() == h_arr[i]){

                        $(this).attr("selected","selected");
                    }
                })
            }

            //选中标签
            var hlebalsval = $("#lebals").val();
            var hlebalsvalarr= new Array(); //定义一数组
            hlebalsvalarr = hlebalsval.split(",");
            for (i=0;i<hlebalsvalarr.length ;i++ )
            {
                $("#labelval input[value="+ hlebalsvalarr[i] +"]").attr("checked","checked");
            }

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },

        }
    };
    return Controller;
});
